//
//  NameRepository.m
//  迭代器模式
//
//  Created by flannery on 2018/12/17.
//  Copyright © 2018年 flannery. All rights reserved.
//

#import "NameRepository.h"

@interface NameRepository() {
}
@property (nonatomic, strong) NSArray *names;
@end
@implementation NameRepository

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.names = [NSArray arrayWithObjects:@"Robert", @"John",@"Julie", @"Lora", nil];
    }
    return self;
}


- (nonnull id<Iterator>)getIterator {
    return [[NameIterator alloc] initWithArray:self.names];
}

@end


#pragma =====NameIterator=====
@interface NameIterator ()
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, assign) NSInteger index;
@end
@implementation NameIterator

- (instancetype)initWithArray:(NSArray*)array
{
    self = [super init];
    if (self) {
        self.array = array;
    }
    return self;
}

- (BOOL)hasNext {
    if (self.index < self.array.count) {
        return YES;
    }
    return NO;
}

- (nonnull id)next {
    if([self hasNext]) {
        return self.array[self.index++];
    }
    return  nil;
}

@end
